Ext.define('AgentInfo', {
    extend: 'Ext.data.Model',
    fields: [
        "Id"
		, "ChineseFullName"   //企业中文名称
		, "AuthorizationID"   //授权ID
		, "Area"   //地区	
        , "AreaCode" // 地区编号	
		, "BankNumber"   //工商注册号
		, "RegisterRoom"   //注册地
		, "Remark"   //备注
		, "Property"   //性质
		, { name: "Scope", type: "int"}   //规模
		, "HomePage"   //主页
		, "HigherUnit"   //上级单位		
		, "WorkPlace"   //办公所在地
		, "CompanyPlace"   //公司所在地
	],
    idProperty: "Id"
});

Ext.define('SHApp.CompanyManage.AgentInfo.Grid', {
    extend: 'BLBTSystem.grid',
    alias: 'shapp.agentInfo.grid',
    requires: ['SHApp.CompanyManage.CompanyInfo.Form',
    'SHApp.System.AdministrativeArea.AreaComboBox'
     ],
    txtChineseFullNameID: 'txtChineseFullName',
    btnAgentProudctID: '_btnAgentProudctID',
    dataModel: "AgentInfo",
    dataUrl: "api/agentInfo?format=json",
    tree: null,
    panel: null,
    initComponent: function () {
        var me = this;
        Ext.apply(this);
        me.btnAddID += me.id;
        me.btnEditID += me.id;
        me.btnDeleteID += me.id;
        me.btnAgentProudctID += me.id;
        me.txtChineseFullNameID += me.id;
        this.creatTbar();
        this.creatColumns();
        this.callParent(arguments);
        this.checkBtnPermission(me.pvalue);
    },
    listeners: {
        selectionchange: function (sel, rs, opts) {
            if (this.down("#" + this.btnAgentProudctID) != null) {
                this.down("#" + this.btnAgentProudctID).setDisabled(rs.length == 0);
            }
        }
    },
    creatColumns: function () {
        var me = this;
        me.columns = [{
            text: "企业名称",
            width: 100,
            dataIndex: 'ChineseFullName',
            sortable: true
        }, {
            text: "地区",
            width: 100,
            dataIndex: 'Area',
            sortable: true
        }, {
            text: "工商注册号",
            width: 100,
            dataIndex: 'BankNumber',
            sortable: true
        }, {
            text: "注册地",
            width: 100,
            dataIndex: 'RegisterRoom',
            sortable: true
        }, {
            text: "性质",
            width: 100,
            dataIndex: 'Property',
            sortable: true
        }, {
            text: "规模",
            width: 100,
            dataIndex: 'Scope',
            sortable: true
        }, {
            text: "主页",
            width: 100,
            dataIndex: 'HomePage',
            sortable: true
        }, {
            text: "上级单位",
            width: 100,
            dataIndex: 'HigherUnit',
            sortable: true
        }, {
            text: "办公所在地",
            width: 100,
            dataIndex: 'WorkPlace',
            sortable: true
        }, {
            text: "公司所在地",
            width: 100,
            dataIndex: 'CompanyPlace',
            sortable: true
        }, {
            text: "备注",
            width: 100,
            dataIndex: 'Remark',
            sortable: true
        }]
    },
    creatTbar: function () {
        var me = this;
        me.tbar = [
		    {
		        xtype: "textfield",
		        fieldLabel: '企业名称:',
		        labelWidth: 60,
		        name: 'ChineseFullName',
		        id: me.txtChineseFullNameID,
		        width: 200
		    }, ' ', {
		        xtype: 'button',
		        text: '查询',
		        iconCls: 'icon-btn-search',
		        handler: function () {
		            me.clearFilters();
		            var vChineseFullName = me.down("#" + me.txtChineseFullNameID).getValue();
		            if (vChineseFullName && vChineseFullName.length > 0) {
		                me.addFilter("ChineseFullName", vChineseFullName);
		            }
		            me.loadByFilter();
		        }
		    }, '->', {
		        xtype: 'button',
		        text: '添加',
		        id: me.btnAddID,
		        iconCls: 'icon-btn-add',
		        handler: function () {
		            var win = me.getForm('SHApp.CompanyManage.AgentInfo.Form');
		            win.loadRecord(new AgentInfo({ Id: null }));
		            win.Show("添加");
		        }
		    }, {
		        xtype: 'button',
		        disabled: true,
		        id: me.btnEditID,
		        text: '修改',
		        iconCls: 'icon-btn-edit',
		        handler: function () {
		            var rs = me.getSelectionModel().getSelection();
		            if (rs) {
		                if (rs.length > 0) {
		                    // rs[0].data.StartDate = formatDate(rs[0].data.StartDate);                           
		                    me.EditData(me.getForm('SHApp.CompanyManage.AgentInfo.Form'));
		                }
		            }
		        }
		    }, ' ', {
		        xtype: 'button',
		        disabled: true,
		        id: me.btnDeleteID,
		        text: '删除',
		        iconCls: 'icon-btn-delete',
		        handler: function () {
		            me.DeleteData();
		        }
		    }, ' ', {
		        xtype: 'button',
		        disabled: true,
		        text: '委托产品',
		        id: me.btnAgentProudctID,
		        iconCls: 'icon-grid',
		        handler: function () {
		            var rs = me.getSelectionModel().getSelection();
		            if (rs != null && rs.length > 0) {
		                me.showProduct(rs[0].data.Id, rs[0].data.ChineseFullName);
		            }
		        }
		    }
        ]
    },
    showProduct: function (id, title) {
        var me = this;
        var win = Ext.create("Ext.window.Window", {
            width: 800,
            height: 500,
            modal: true,
            padding: "10 10",
            layout: "border",
            // autoScroll: true,
            items: [
                me.createTree(id, title),
                me.createPanel(id, title)
            ]
        });
        win.setTitle(title + "-委托产品管理");
        win.show();
    },
    createTree: function (vagentId, vagentName) {
        var me = this;
        var treePanel = Ext.create('SHApp.CompanyManage.AgentProduct.Tree', {
            title: '委托城市',
            width: 222,
            height: "100%",
            split: true,
            margins: '0 0 25 0',
            region: 'west',
            listeners: {
                itemclick: function (view, record, item, index, e, eOpts) {
                    if (record != null) {
                        me.panel.loadDataByCity(vagentId, vagentName, record.data.CityCode);
                    }
                }
            }
        });
        me.tree = treePanel;
        return treePanel;
    },
    createPanel: function (vid, vtitle) {
        var me = this;
        var panel = Ext.create("SHApp.CompanyManage.AgentProduct.Grid", {
            agentId: vid,
            agentName: vtitle,
            region: "center",
            title: '提供产品'
        });
        me.panel = panel;
        return panel;
    }
})

